package com.vf.admin.client.controller.ip;

import com.alibaba.fastjson.JSONObject;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.utils.pay.ApiAplayWeChat.IpUtils;
import okhttp3.*;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : NemoMeng
 * @Date 2023/12/13 17:13
 */
@Controller
@RequestMapping(value = "/api/ipGetAddress")
public class IpGetAddressController extends ClientBaseController {

    private static CloseableHttpClient httpClient = null;

    @RequestMapping("/getAddress")
    @ResponseBody
    public Result isWhiteIp(String ip) throws IOException {

        String realIp = IpUtils.getRealIp(request);


        String url = "https://api.shuzike.com/lifeservice/check/80";
        String userAuthKey = "83928ab7-0109-4afa-8a8c-0301c5ef47eb876";

        //请求URL
//        HttpPost httpPost = new HttpPost(url);
        // 请求body参数
//        String reqdata = "{\"ip\"："+ ip+ "}";

//
//        StringEntity entity = new StringEntity(reqdata, "utf-8");
//        entity.setContentType("application/json");
//        httpPost.setEntity(entity);
//        httpPost.setHeader("Content-Type", "application/json");
//        httpPost.setHeader("UserAuthKey", userAuthKey);
//
//        CloseableHttpResponse response = httpClient.execute(httpPost);

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject postData = new JSONObject();
        postData.put("ip",realIp);

        RequestBody body = RequestBody.create(mediaType, postData.toString());
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("UserAuthKey", userAuthKey)
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(request);
        JSONObject jsonObject1 = JSONObject.parseObject(response.body().string());
        return this.sucessJSON(jsonObject1);


    }
}
